/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAOs;

import DAOs.exceptions.NonexistentEntityException;
import Entity.Vivarium;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Entity.VivariumOperator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author USUARIO
 */
public class VivariumJpaController implements Serializable {

    public VivariumJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Vivarium vivarium) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            VivariumOperator vivariumOperator = vivarium.getVivariumOperator();
            if (vivariumOperator != null) {
                vivariumOperator = em.getReference(vivariumOperator.getClass(), vivariumOperator.getId());
                vivarium.setVivariumOperator(vivariumOperator);
            }
            em.persist(vivarium);
            if (vivariumOperator != null) {
                vivariumOperator.getUlataVivariums().add(vivarium);
                vivariumOperator = em.merge(vivariumOperator);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Vivarium vivarium) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Vivarium persistentVivarium = em.find(Vivarium.class, vivarium.getId());
            VivariumOperator vivariumOperatorOld = persistentVivarium.getVivariumOperator();
            VivariumOperator vivariumOperatorNew = vivarium.getVivariumOperator();
            if (vivariumOperatorNew != null) {
                vivariumOperatorNew = em.getReference(vivariumOperatorNew.getClass(), vivariumOperatorNew.getId());
                vivarium.setVivariumOperator(vivariumOperatorNew);
            }
            vivarium = em.merge(vivarium);
            if (vivariumOperatorOld != null && !vivariumOperatorOld.equals(vivariumOperatorNew)) {
                vivariumOperatorOld.getUlataVivariums().remove(vivarium);
                vivariumOperatorOld = em.merge(vivariumOperatorOld);
            }
            if (vivariumOperatorNew != null && !vivariumOperatorNew.equals(vivariumOperatorOld)) {
                vivariumOperatorNew.getUlataVivariums().add(vivarium);
                vivariumOperatorNew = em.merge(vivariumOperatorNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = vivarium.getId();
                if (findVivarium(id) == null) {
                    throw new NonexistentEntityException("The vivarium with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Vivarium vivarium;
            try {
                vivarium = em.getReference(Vivarium.class, id);
                vivarium.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The vivarium with id " + id + " no longer exists.", enfe);
            }
            VivariumOperator vivariumOperator = vivarium.getVivariumOperator();
            if (vivariumOperator != null) {
                vivariumOperator.getUlataVivariums().remove(vivarium);
                vivariumOperator = em.merge(vivariumOperator);
            }
            em.remove(vivarium);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Vivarium> findVivariumEntities() {
        return findVivariumEntities(true, -1, -1);
    }

    public List<Vivarium> findVivariumEntities(int maxResults, int firstResult) {
        return findVivariumEntities(false, maxResults, firstResult);
    }

    private List<Vivarium> findVivariumEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Vivarium.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Vivarium findVivarium(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Vivarium.class, id);
        } finally {
            em.close();
        }
    }

    public int getVivariumCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Vivarium> rt = cq.from(Vivarium.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
